from telethon import TelegramClient, events
from colorama import Fore
from pathlib import Path
import asyncio
import csv

api_id = 
api_hash = ''

try:
    with open('data.csv', 'r') as file:
            csvreader1 = csv.reader(file)
        
            for row1 in csvreader1:
                out_c10 = (row1[0])
            
    with open('data.csv', 'r') as file:
            csvreader1 = csv.reader(file)
            for row2 in csvreader1:
                inp_c10 = (row2[1])

    print(Fore.LIGHTMAGENTA_EX+"Your config of data.csv: "+"\033[39m")
    print(Fore.LIGHTWHITE_EX+"________________________________________")
    print(Fore.LIGHTCYAN_EX+"\nInput channel"+"\n"+inp_c10)
    print("\n"+Fore.LIGHTCYAN_EX+"\nOutput channel"+"\n"+out_c10)
    print(Fore.LIGHTWHITE_EX+"________________________________________")
except FileNotFoundError:
    pass
writing_csv = input("\n"+Fore.LIGHTGREEN_EX+"yes\nno\nl "+Fore.LIGHTYELLOW_EX+"= load previous configuration of REZdata"+"\033[39m"+Fore.LIGHTMAGENTA_EX+"\nDo you want to load the previous configuration?: "+"\033[39m")

def if_writing1():
    if writing_csv == "no":
        print(Fore.LIGHTGREEN_EX+'ID - к примеру -11111111'+'\033[39m')
        out_c = int(input(Fore.LIGHTMAGENTA_EX+'ID канала куда будут кидаться посты: '+'\033[39m'))
        inp_string=input(Fore.LIGHTGREEN_EX+'Несколько каналов можно через ,\n'+Fore.LIGHTMAGENTA_EX+'ID каналов откуда будут браться посты: '+'\033[39m')

        lsttt=[]
        b=(inp_string.split(','))
        for x in b:
            lsttt.append(int(x))


        double_ch = [
            [out_c, inp_string],
            ]
        with open('data.csv', 'w', encoding='cp1251') as file:
            writer = csv.writer(file, delimiter=',')
            writer.writerow(
                ("1_ch", "2_ch")
                )

        for users in double_ch:
            with open('data.csv', 'a', encoding='cp1251') as file:
                writer = csv.writer(file, delimiter=',')
                writer.writerow(
                    users
                   )
                
    # Резервная копия

        path_to_file = 'REZdata.csv'
        path = Path(path_to_file)

        if path.is_file():
            with open('REZdata.csv', 'r') as file:
                    csvreader1 = csv.reader(file)
                    
                    for row1 in csvreader1:
                        out_c6 = (row1[1])
                        
            with open('REZdata.csv', 'r') as file:
                    csvreader1 = csv.reader(file)
                    for row2 in csvreader1:
                        out_c7 = (row2[0])

            print(Fore.LIGHTMAGENTA_EX+"Your config of data.csv: "+"\033[39m")
            print(Fore.LIGHTWHITE_EX+"________________________________________")
            print(Fore.LIGHTCYAN_EX+"\nInput channel"+"\n"+out_c6)
            print("\n"+Fore.LIGHTCYAN_EX+"\nOutput channel"+"\n"+out_c7)
            print(Fore.LIGHTWHITE_EX+"________________________________________")
            rez = input(Fore.LIGHTGREEN_EX+"Yes\nno"+"\033[39m"+Fore.LIGHTMAGENTA_EX+"\nDo you want to rewriting your REZdata?: "+"\033[39m")
            
            if rez == "no":
                pass
            if rez == "Yes":
                with open('REZdata.csv', 'w', encoding='cp1251') as file:
                    writer = csv.writer(file, delimiter=',')
                    writer.writerow(
                        ("1_ch", "2_ch")
                        )

                for users in double_ch:
                    with open('REZdata.csv', 'a', encoding='cp1251') as file:
                        writer = csv.writer(file, delimiter=',')
                        writer.writerow(
                            users
                           )
        else:
            with open('REZdata.csv', 'w', encoding='cp1251') as file:
                writer = csv.writer(file, delimiter=',')
                writer.writerow(
                    ("1_ch", "2_ch")
                    )

            for users in double_ch:
                with open('REZdata.csv', 'a', encoding='cp1251') as file:
                    writer = csv.writer(file, delimiter=',')
                    writer.writerow(
                        users
                       )

        my_channel_id=out_c
        channels=b

        result = [int(item) for item in lsttt]

        my_channel_id=out_c
        channels=result



        client = TelegramClient('myGrab', api_id, api_hash)
        print("GRAB - Started")

        @client.on(events.NewMessage(chats=channels))
        async def my_event_handler(event):
            if event.message:
                await client.send_message(my_channel_id, event.message)

        client.start()
        client.run_until_disconnected()
if_writing1()


def if_writing2():
    if writing_csv == "yes":
        with open('data.csv', 'r') as file:
                csvreader = csv.reader(file)
                
                for row in csvreader:
                    out_c = (row[0])


        with open('data.csv', 'r') as file:
                csvreader = csv.reader(file)
                
                for row in csvreader:
                    inp_c = (row[1])
        out_c = int(out_c)
        lsttt=[]
        b=(inp_c.split(','))
        for x in b:
            lsttt.append(int(x))

        my_channel_id=out_c
        channels=b

        result = [int(item) for item in lsttt]
   
        my_channel_id=out_c
        channels=result


        client = TelegramClient('myGrab', api_id, api_hash)
        print("GRAB - Started")

        @client.on(events.NewMessage(chats=channels))
        async def my_event_handler(event):
            if event.message:
                await client.send_message(my_channel_id, event.message)

        client.start()
        client.run_until_disconnected()
if_writing2()

def if_writing3():
    if writing_csv == "l":
        with open('REZdata.csv', 'r') as file:
                csvreader1 = csv.reader(file)
                
                for row in csvreader1:
                    out_c = (row[0])
                    
        with open('REZdata.csv', 'r') as file:
                csvreader2 = csv.reader(file)
                for row in csvreader2:
                    inp_c = (row[1])
        
        print("\n"+Fore.LIGHTMAGENTA_EX+"Your config of REZdata.csv: "+"\033[39m")
        print(Fore.LIGHTWHITE_EX+"________________________________________")
        print(Fore.LIGHTCYAN_EX+"\nInput channel"+"\n"+inp_c)
        print("\n"+Fore.LIGHTCYAN_EX+"\nOutput channel"+"\n"+out_c)
        print(Fore.LIGHTWHITE_EX+"________________________________________")
        
        rez = input(Fore.LIGHTGREEN_EX+"yes\nno"+Fore.LIGHTMAGENTA_EX+"\nDo you want to load REZdata?: "+"\033[39m")
        if rez == "no":
            exit()
        if rez == "yes":
            out_c = int(out_c)
            lsttt=[]
            b=(inp_c.split(','))
            for x in b:
                lsttt.append(int(x))

            my_channel_id=out_c
            channels=b

            result = [int(item) for item in lsttt]
       
            my_channel_id=out_c
            channels=result


            client = TelegramClient('myGrab', api_id, api_hash)
            print("GRAB - Started")

            @client.on(events.NewMessage(chats=channels))
            async def my_event_handler(event):
                if event.message:
                    await client.send_message(my_channel_id, event.message)

            client.start()
            client.run_until_disconnected()

            
if_writing3()